#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__ = 'menangen'

import cgi, cgitb, os, couch_controller
import tenjin
from tenjin.helpers import *

cgitb.enable()

lang = os.environ["HTTP_ACCEPT_LANGUAGE"][6:8]

print "Content-Type: text/html"
print

FS = cgi.FieldStorage()
if "nik" not in FS or "text" not in FS:
    try:
        couchData = couch_controller.getAll()
        #print couchData
    except :
        print("CouchDB not started!")
        couchData = None

    context = {'data':couchData}
    #print context
    engine = tenjin.Engine(path=['views'])
    html = engine.render('contact.html', context)
    print(html)

else:
    nik = FS["nik"].value
    comment = FS["text"].value

    try:
        result = couch_controller.saveComment(nik, comment)

        if result:
            print "<p/>Saved!"
        else:
            print "<p/>Error!"

    except:
        print "Sorry! Couch Server error!"
